package vin

import com.alibaba.fastjson.JSON
import com.showapi.app.dynamic.BaseLine
import com.showapi.app.util.CommonUtil

class VinLine extends BaseLine {
	//    http://localhost:8080/flow/vin.VinLine?vin=ldc973z43k1017328


	@Override
	public  Object	doVerification(Map para) {
		def vin=para.vin?.trim().toLowerCase()
		if(!vin||vin.size()!=17){return  getRetObj(-1,'vin码长度为17位!')}
		para.vin=vin

		def new_vin= vin.substring(0,8)+"0"+vin.substring(9)//换成新的vin，因为第9位是校验位，都设置为0即可
		def code11=new_vin.substring(0,11)
		para.code11=code11

		def cache_key="cxt:vinservice_"+ code11
		int c=redisService.incr(cache_key)
		if(c==1){
			redisService.expire(cache_key,5)
		}else{
			int cnt=1
			while(true){
				cnt++
				if(cnt++>20){
					return  getRetObj(-1,'request limit')
				}
				Thread.sleep(1000)
				c=redisService.incr(cache_key)
				if(c==1){
					redisService.expire(cache_key,5)
					break
				}
			}
		}

		return null
	}


	@Override
	public boolean doStopLineCondition(Map actionResult,Map reqParams,Map lineParams) {
		if(actionResult)return true
		else return false
	}

	@Override
	public Map doAfterLine(Map lastActionResult,Map para,  Map lineParams) {
		def cache_key="cxt:vinservice_"+ para.code11
		redisService.flushCache(cache_key)  //清除计数器
		if(lineParams.shareMap.get("notLegal")){//说明非法验证不通过
			return    getRetObj(-1,'vin码不符合标准规范!')
		}
		//运行到这里，lastActionResult肯定保底有值
		lastActionResult.vin=para.vin
		lastActionResult.code=para.code11
		lastActionResult.code8=para.code11.substring(0,8)

		println "in do_after  "
		removeProperty(lastActionResult,["_id", "ct", "from", "code", "code8", "checkd_data" ]) //checkd_data表示是否从haoservice验证过
		return lastActionResult

	}

	@Override
	public String doGetPackageName() {
		return "vin";
	}
 
}
